home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / msm-1 / icont.sit / trans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-19  |  4.8 KB  |  202 lines  |  [TEXT/MPS ]

  1. /*
  2.  * trans.c - main control of the translation process.
  3.  */
  4.  
  5. #include "::h:gsupport.h"
  6. #include "tproto.h"
  7. #include "::h:version.h"
  8. #include "globals.h"
  9. #include "trans.h"
  10. #include "tsym.h"
  11. #include "tree.h"
  12. #include "token.h"
  13.  
  14. /*
  15.  * Prototypes.
  16.  */
  17.  
  18. hidden    FILE    *preprocess    Params((char *filename));
  19. hidden    novalue    trans1        Params((char *filename));
  20.  
  21. int tfatals;            /* number of fatal errors in file */
  22. int afatals;            /* total number of fatal errors */
  23. int nocode;            /* non-zero to suppress code generation */
  24. int in_line;            /* current input line number */
  25. int incol;            /* current input column number */
  26. int peekc;            /* one-character look ahead */
  27.  
  28. FILE *srcfile;            /* current input file */
  29. FILE *codefile;            /* current ucode output file */
  30. FILE *globfile;            /* current global table output file */
  31.  
  32. /*
  33.  * translate a number of files, returning an error count
  34.  */
  35. int trans(ifiles)
  36. char **ifiles;
  37.    {
  38.    tmalloc();            /* allocate memory for translation */
  39.  
  40.    afatals = 0;
  41.  
  42. #ifdef MultipleRuns
  43.    yylexinit();            /* initialize lexical analyser */
  44.    tcodeinit();            /* initialize code generator */
  45. #endif                    /* Multiple Runs */
  46.  
  47.    while (*ifiles) {
  48.       trans1(*ifiles++);    /* translate each file in turn */
  49.       afatals += tfatals;
  50.       }
  51.    tmfree();            /* free memory used for translation */
  52.  
  53.    /*
  54.     * Report information about errors and warnings and be correct about it.
  55.     */
  56.    if (afatals == 1)
  57.       fprintf(stderr, "1 error\n");
  58.    else if (tfatals > 1)
  59.       fprintf(stderr, "%d errors\n", afatals);
  60.    else if (!silent)
  61.       fprintf(stderr, "No errors\n");
  62.  
  63.    return afatals;
  64.    }
  65.  
  66. /*
  67.  * translate one file.
  68.  */
  69. static novalue trans1(filename)
  70. char *filename;
  71. {
  72.    char oname1[MaxFileName];    /* buffer for constructing file name */
  73.    char oname2[MaxFileName];    /* buffer for constructing file name */
  74.  
  75.    tfatals = 0;    /* reset error counts */
  76.    nocode = 0;            /* allow code generation */
  77.    in_line = 1;            /* start with line 1, column 0 */
  78.    incol = 0;
  79.    peekc = 0;            /* clear character lookahead */
  80.  
  81.    if (m4pre)
  82.       srcfile = preprocess(filename);
  83.    else if (strcmp(filename,"-") == 0) {
  84.       srcfile = stdin;
  85.       filename = "stdin";
  86.       }
  87.    else
  88.       srcfile = fopen(filename,ReadText);
  89.    if (srcfile == NULL)
  90.       quitf("cannot open %s",filename);
  91.    if (!silent)
  92.       fprintf(stderr, "%s:\n",filename);
  93.  
  94. #ifndef VarTran
  95.    /*
  96.     * Form names for the .u1 and .u2 files and open them.
  97.     *  Write the ucode version number to the .u2 file.
  98.     */
  99.  
  100.    makename(oname1, TargetDir, filename, U1Suffix);
  101.  
  102. #if MVS || VM
  103. /*
  104.  * Even though the ucode data is all reasonable text characters, use
  105.  *  of text I/O may cause problems if a line is larger than LRECL.
  106.  *  This is likely to be true with any compiler, though the precise
  107.  *  disaster which results may vary.
  108.  *
  109.  * On CMS (and sometimes on MVS), empty files are not readable later.
  110.  *  Since the .U1 file may be empty, we start it off with an extra
  111.  *  blank (thrown away on input) to make sure there's something there.
  112.  */
  113.    codefile = fopen(oname1, WriteBinary);   /* avoid line splits */
  114.    if (codefile != NULL)
  115.       putc(' ', codefile);
  116. #else                    /* MVS || VM */
  117.    codefile = fopen(oname1, WriteText);
  118. #endif                    /* MVS || VM */
  119.  
  120.    if (codefile == NULL)
  121.       quitf("cannot create %s", oname1);
  122.  
  123.    makename(oname2, TargetDir, filename, U2Suffix);
  124.  
  125. #if MVS || VM
  126.    globfile = fopen(oname2, WriteBinary);
  127. #else                    /* MVS || VM */
  128.    globfile = fopen(oname2, WriteText);
  129. #endif                    /* MVS || VM */
  130.  
  131.    if (globfile == NULL)
  132.       quitf("cannot create %s", oname2);
  133.    writecheck(fprintf(globfile,"version\t%s\n",UVersion));
  134. #endif                    /* VarTran */
  135.  
  136.    tok_loc.n_file = filename;
  137.    in_line = 1;
  138.  
  139.    tminit();                /* Initialize data structures */
  140.    yyparse();                /* Parse the input */
  141.  
  142.    /*
  143.     * Close the output files and the input file.
  144.     */
  145.  
  146. #ifndef VarTran
  147.    if (fclose(codefile) != 0 || fclose(globfile) != 0)
  148.       quit("cannot close ucode file");
  149. #endif                    /* VarTran */
  150.  
  151.    if (!m4pre) 
  152.       fclose(srcfile);
  153.    /* "else" is below in conditional */
  154.  
  155. #if ARM || UNIX
  156.    else if (pclose(srcfile) != 0)
  157.       quit("m4 terminated abnormally");
  158. #endif                    /* ARM || UNIX */
  159.  
  160.    if (tfatals) {
  161.       unlink(oname1);
  162.       unlink(oname2);
  163.       }
  164.    }
  165.  
  166. /*
  167.  * writecheck - check the return code from a stdio output operation
  168.  */
  169. novalue writecheck(rc)
  170. int rc;
  171.    {
  172.    if (rc < 0)
  173.       quit("cannot write to ucode file");
  174.    }
  175.  
  176. /*
  177.  * open a pipe to the preprocessor.
  178.  */
  179. static FILE *preprocess(filename)
  180. char *filename;
  181. {
  182.  
  183. #if MACINTOSH
  184. #if MPW
  185. /* #pragma unused(filename) */
  186. #endif                    /* MPW */
  187. #endif                    /* MACINTOSH */
  188.  
  189. #if ARM || UNIX
  190.       {
  191.       FILE *f, *popen();
  192.       char *s = alloc((unsigned int)(4+strlen(filename)));
  193.       sprintf(s,"m4 %s",filename);
  194.       f = popen(s,ReadText);
  195.       free(s);
  196.       return f;
  197.       }
  198. #else                    /* ARM || UNIX */
  199.    return NULL;
  200. #endif                    /* ARM || UNIX */
  201. }
  202.